package cz.semjob.linguistic.reader.factory.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import cz.semjob.common.attribute.value.NominalStringAttributeValue;
import cz.semjob.linguistic.reader.LinguisticFormatProcessingException;
import cz.semjob.linguistic.reader.attribute.ACaseAttribute;
import cz.semjob.linguistic.reader.attribute.AGenderAttribute;
import cz.semjob.linguistic.reader.attribute.ANumberAttribute;
import cz.semjob.linguistic.reader.attribute.APartOfSpeechAttribute;
import cz.semjob.linguistic.reader.attribute.value.linguistic.PartOfSpeechAttributeValue;
import cz.semjob.linguistic.reader.factory.IPartOfSpeechFactory;

@Component
public class PartOfSpeechFactory implements IPartOfSpeechFactory {

	@Autowired
	private APartOfSpeechAttribute partOfSpeechAttribute;

	@Autowired
	private ACaseAttribute caseAttribute;

	@Autowired
	private ANumberAttribute numberAttribute;

	@Autowired
	private AGenderAttribute genderAttribute;

	public PartOfSpeechAttributeValue createAttribute(String inputString)
			throws LinguisticFormatProcessingException {
		if (inputString == null || inputString.length() != 15) {
			throw new LinguisticFormatProcessingException(
					"Invalid input string");
		}
		PartOfSpeechAttributeValue posTag = new PartOfSpeechAttributeValue();
		// pos
		String pos = partOfSpeechAttribute
				.getValue(inputString.substring(0, 1));
		if (pos == null) {
			throw new LinguisticFormatProcessingException("Invalid POS tag: "
					+ inputString.substring(0, 1));
		}
		posTag.addAttrValue(new NominalStringAttributeValue("POS", pos, partOfSpeechAttribute));
		// number
		String number = numberAttribute.getValue(inputString.substring(3, 4));
		if (number == null) {
			throw new LinguisticFormatProcessingException(
					"Invalid Number tag: " + inputString.substring(3, 4));
		}
		posTag.addAttrValue(new NominalStringAttributeValue("Number",
				number, numberAttribute));
		// case
		String wordcase = caseAttribute.getValue(inputString.substring(4, 5));
		if (wordcase == null) {
			throw new LinguisticFormatProcessingException("Invalid Case tag: "
					+ inputString.substring(4, 5));
		}
		posTag.addAttrValue(new NominalStringAttributeValue("Case",	wordcase, caseAttribute));
		// gender
		String gender = genderAttribute.getValue(inputString.substring(2, 3));
		if (gender == null) {
			throw new LinguisticFormatProcessingException("Invalid Gender tag: "
					+ inputString.substring(2, 3));
		}
		posTag.addAttrValue(new NominalStringAttributeValue("Gender", gender, genderAttribute));
		return posTag;
	}

	public void setPartOfSpeechAttribute(
			APartOfSpeechAttribute partOfSpeechAttribute) {
		this.partOfSpeechAttribute = partOfSpeechAttribute;
	}

	public APartOfSpeechAttribute getLanguageConfigPartOfSpeechAttribute() {
		return partOfSpeechAttribute;
	}

	public ACaseAttribute getLanguageConfigCaseAttribute() {
		return caseAttribute;
	}

	public ANumberAttribute getLanguageConfigNumberAttribute() {
		return numberAttribute;
	}
	
	public AGenderAttribute getGenderAttribute() {
		return genderAttribute;
	}

}
